package com.example.wechat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wechat.database.WeChatDatabaseManager;

public class ProfileActivity extends Activity {
    private static final int REQUEST_CODE_PICK_IMAGE = 1;
    private ImageView imgAvatar;
    private TextView tvNickname, tvSignature;
    private Button btnEditNickname, btnEditSignature;
    private WeChatDatabaseManager dbManager;
    private int currentUserId = 1; // 假设当前用户ID为1
    private String avatarPath = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        dbManager = new WeChatDatabaseManager(this);
        dbManager.open();
        imgAvatar = findViewById(R.id.img_avatar);
        tvNickname = findViewById(R.id.tv_nickname);
        tvSignature = findViewById(R.id.tv_signature);
        btnEditNickname = findViewById(R.id.btn_edit_nickname);
        btnEditSignature = findViewById(R.id.btn_edit_signature);

        // 加载用户信息
        loadUserInfo();

        imgAvatar.setOnClickListener(v -> pickImage());
        btnEditNickname.setOnClickListener(v -> editNickname());
        btnEditSignature.setOnClickListener(v -> editSignature());
    }

    private void loadUserInfo() {
        // 这里只做演示，实际应从数据库读取
        // avatarPath = dbManager.getUserAvatar(currentUserId);
        // String nickname = dbManager.getUserNickname(currentUserId);
        // String signature = dbManager.getUserSignature(currentUserId);
        String nickname = "微信用户";
        String signature = "这个人很懒，什么都没写";
        if (avatarPath != null) {
            imgAvatar.setImageURI(Uri.parse(avatarPath));
        }
        tvNickname.setText(nickname);
        tvSignature.setText(signature);
    }

    private void pickImage() {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            imgAvatar.setImageURI(selectedImage);
            avatarPath = selectedImage.toString();
            // dbManager.updateUserAvatar(currentUserId, avatarPath);
        }
    }

    private void editNickname() {
        final EditText input = new EditText(this);
        new AlertDialog.Builder(this)
                .setTitle("编辑昵称")
                .setView(input)
                .setPositiveButton("确定", (dialog, which) -> {
                    String newNickname = input.getText().toString().trim();
                    if (!newNickname.isEmpty()) {
                        tvNickname.setText(newNickname);
                        // dbManager.updateUserNickname(currentUserId, newNickname);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void editSignature() {
        final EditText input = new EditText(this);
        new AlertDialog.Builder(this)
                .setTitle("编辑签名")
                .setView(input)
                .setPositiveButton("确定", (dialog, which) -> {
                    String newSignature = input.getText().toString().trim();
                    if (!newSignature.isEmpty()) {
                        tvSignature.setText(newSignature);
                        // dbManager.updateUserSignature(currentUserId, newSignature);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
} 